package com.yungu.swift.admin.route.system;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.admin.consts.AdminErrorEnum;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.common.model.dto.CommonTagDto;
import com.yungu.swift.common.model.enums.CommonErrorEnum;
import com.yungu.swift.common.model.param.AdminTagPageParam;
import com.yungu.swift.common.model.param.AdminTagParam;
import com.yungu.swift.common.service.CommonTagService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.ParamUtil;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : cuixiuyin
 * @date : 2019/6/18
 */
@RestController
@RequestMapping("/admin/v1/system/tag")
@Api(value = "【系统服务】", tags = "标签管理")
public class TagAdmin {

    @Reference
    private CommonTagService commonTagService;

    @RequestMapping(value = "/queryPage", method = RequestMethod.POST)
    @ApiOperation(value = "标签分页查询", httpMethod = "POST")
    public ResponseData<PageVo<CommonTagDto>> queryPage(@RequestBody AdminTagPageParam tagPageParam) {
        return commonTagService.queryPage(tagPageParam);
    }


    @RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "标签新增或者更新", httpMethod = "POST")
    public ResponseData<Boolean> saveOrUpdate(@RequestBody AdminTagParam tagParam) {
        if (ParamUtil.isIllegal(tagParam.getTagCode(), tagParam.getTagName(), tagParam.getTagNameEn())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        if(tagParam.getUuid() == null) {
            //新增时校验编码是否已存在
            ResponseData<List<CommonTagDto>> checkList = commonTagService.list(MapUtils.build("tagCode", tagParam.getTagCode()));
            if (checkList.isSuccess() && checkList.getData().size() > 0) {
                return ResponseData.buildErrorResponse(0, "标签编码已存在，请检查后重新输入");
            }
        }
        return commonTagService.saveOrUpdate(tagParam);
    }

    @RequestMapping(value = "/status", method = RequestMethod.POST)
    @ApiOperation(value = "标签开关", httpMethod = "POST")
    public ResponseData<Boolean> status(@RequestBody AdminTagParam tagParam) {
        if (ParamUtil.isIllegal(tagParam.getTagStatus(), tagParam.getUuid())) {
            return ResponseData.buildErrorResponse(AdminErrorEnum.PARAM_ERROR.getCode(), AdminErrorEnum.PARAM_ERROR.getMsg());
        }
        if(tagParam.getTagStatus() == 1){
            ResponseData checkRsd = checkTagOpen(tagParam);
            if(!checkRsd.isSuccess()){
                return  checkRsd;
            }
        }
        CommonTagDto commonTagDto = new CommonTagDto();
        commonTagDto.setUuid(tagParam.getUuid());
        commonTagDto.setTagStatus(tagParam.getTagStatus());
        return commonTagService.edit(commonTagDto);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除标签", httpMethod = "POST")
    public ResponseData<Boolean> delete(@RequestBody AdminTagParam tagParam) {
        CommonTagDto commonTagDto = new CommonTagDto();
        commonTagDto.setUuid(tagParam.getUuid());
        return commonTagService.del(commonTagDto);
    }

    /**
     * 开启校验
     */
    private ResponseData checkTagOpen(AdminTagParam tagParam){
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("uuid",tagParam.getUuid());
        CommonTagDto commonTagDto = commonTagService.list(paraMap).getData().get(0);
        paraMap.clear();
        paraMap.put("tagStatus",1);
        paraMap.put("tagType",commonTagDto.getTagType());
        paraMap.put("identity",commonTagDto.getIdentity());
        //评价时 需要加上业务星级条件
        if(commonTagDto.getTagType()==1){
            paraMap.put("star",commonTagDto.getStar());
        }else{
            paraMap.put("businessType",commonTagDto.getBusinessType());
        }
        ResponseData<Integer> countRsd = commonTagService.count(paraMap);
        if(!countRsd.isSuccess()){
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE,"查询标签信息失败");
        }
        Integer openCount = countRsd.getData();
        boolean canOpen = true;
        String errorStr ="";
        switch (commonTagDto.getTagType()){
            case 1:
                if(openCount>=4){
                    canOpen = false;
                    errorStr = commonTagDto.getStar()+"星级评价标签";
                }
                break;
            case 2:
                if(openCount>=4){
                    canOpen = false;
                    errorStr = "投诉标签";
                }
                break;
            case 3:
                if(openCount>=5){
                    canOpen = false;
                    errorStr = "捎话标签";
                }
                break;
            case 5:
                if(openCount>=3){
                    canOpen = false;
                    errorStr = "小件类型标签";
                }
                break;
            case 6:
                if(openCount>=3){
                    canOpen = false;
                    errorStr = "小件重量标签";
                }
                break;
            case 7:
                if(openCount>=3){
                    canOpen = false;
                    errorStr = "小件体积标签";
                }
                break;

            default:
                break;
        }
        if(!canOpen){
            String identity = commonTagDto.getIdentity() == 1?"乘客端":"司机端";
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE,identity+errorStr+"开启数量不能超过"+openCount+"个");
        }
        return ResponseData.buildSuccessResponse("校验通过");
    }
}
